logo
down
shadow

Mutex and thread in c


Mutex and thread in c

By : user2185280
Date : November 21 2020, 04:01 AM
I hope this helps you . The threads don't necessarily run until you call pthread_join.
By then you've already printed the output.
code :


Share : facebook icon twitter icon
Is mutex+atomic necessary to make this code thread safe, or is mutex enough?

Is mutex+atomic necessary to make this code thread safe, or is mutex enough?


By : user3244609
Date : March 29 2020, 07:55 AM
seems to work fine You do not need to use atomic variables if all accesses to those variables are protected by a mutex. This is the case in your code, as all public member functions lock addMutex on entry. Therefore addIndex can be a plain int and everything will still work fine. The mutex locking and unlocking ensures that the correct values become visible to other threads in the right order.
std::atomic<> allows concurrent access outside the protection of a mutex, ensuring that threads see correct values of the variable, even in the face of concurrent modifications. If you stick to the default memory ordering it also ensures that each thread reads the latest value of the variable. std::atomic<> can be used to write thread-safe algorithms without mutexes, but is not required if all accesses are protected by the same mutex.
How can any thread usually release the non-recursive mutex no matter which thread originally took the mutex?

How can any thread usually release the non-recursive mutex no matter which thread originally took the mutex?


By : Calvin Zhai
Date : March 29 2020, 07:55 AM
wish of those help This may differ between different thread implementations, but since you've tagged your question with "pthreads" I assume you're interested in pthread mutexes (and not vxworks mutexes, which is apparently what the link you provide describes).
So in pthreads the rule is that the same thread that locks a mutex must unlock it. You can set attributes on the mutex object whether you want an error to be generated if this rule is violated, or whether the result is undefined behavior (say, for debug vs. release builds). See the manpage for the pthread_mutexattr_settype function for details.
mutex lock in multi-thread can I use several mutex locks

mutex lock in multi-thread can I use several mutex locks


By : Diego Santos
Date : March 29 2020, 07:55 AM
I hope this helps you . I want to implement a function that I create 5 pairs threads(one pair means one thread write and the other read and both of the threads share one list (5 lists in this scenario). Do I need to create five mutex locks? How to declare them? In global area? ,
Do I need to create five mutex locks?
code :
class MyAsynchDataProvider
{
public:

    void run()
    {
        writeThread = std::thread(writeDataFunc,this);
    }

    MyDataStruct getSafeDataCopy()
    {
        std::lock_guard lock(dataGuard);
        return data;
    }

private:
    std::mutex dataGuard;
    MyDataStruct data;

    std::thread writeThread;

    static void writeThreadFunc(MyDataWorker* thisPtr)
    {
        // ...
        std::lock_guard lock(thisPtr->dataGuard);
        // Write to thisPtr->data member
    }
};
Is it better to have a conditional variable and a mutex or just a mutex in a 2 thread situation

Is it better to have a conditional variable and a mutex or just a mutex in a 2 thread situation


By : James Fisher
Date : March 29 2020, 07:55 AM
will be helpful for those in need In this case, it doesn't much matter because it takes such a short time to count k to 100.
If, however, you were doing something that took some time, the 2nd would be more appropriate unless you knew, for sure, that k would have to reach 100 before anything happened.
Does thread need to get the ownership of the mutex to close the handle of the mutex

Does thread need to get the ownership of the mutex to close the handle of the mutex


By : Nguyễn Huy Quyết
Date : March 29 2020, 07:55 AM
it should still fix some issue No, you do not need to own the mutex to close the handle. That would be an unreasonable requirement, since it may not be possible to claim ownership of the mutex, e.g., if another process holds it.
Also note that you cannot release the mutex without the handle.
Related Posts Related Posts :
  • Making conversion similar to BN_hex2bn + BN_bn2bin without openSSL in C
  • I don't understand how the time complexity for this algorithm is calculated
  • MACRO in LHS or RHS of == (!=) operator inside if() condition in C?
  • C: Initialize array of character pointers (strings) and iterate through a file using fgets to put value in these strings
  • extended ASCII characters not working in int 10h
  • how to handle a mathematical function in C language like MATLAB
  • Random derangement memory leak
  • C POSIX ERE without back-references
  • Will disabling interrupts protect a non-volatile variable or can reordering happen?
  • define 2D array dimensions by random numbers in C
  • How to tell if FILE* is referring to a directory?
  • Use printf for ternary operator arguments with mixed types
  • Use of function pointers in C on data structure development
  • Determine Cpu cache associativity
  • Endianness and shift operator in C language, am I doing it correctly?
  • How to use `strtoul` to parse string where zero may be valid?
  • Segmentation fault every third running my program, using mallloc
  • ATM withdrawing money using only 50s and 20s bills C
  • Alphabets not appearing from array (Strange characters appearing)-C programming
  • Rpn calculator: How to free an element that was popped from the stack?
  • CreateFile over USB HID device fails with Access Denied (5) since Windows 10 1809
  • C, minus operator in multiply
  • Is cache invalidation promised in this implementation
  • Assigning memory size dynamically causes stack smashing error
  • Why does makefile recompiles everything when a headerfile is changed?
  • Segmentation Fault while copying unsigned int values in C
  • visual studio code debug C request timeout
  • Understanding indirection through pointers and taking address
  • How can I delete extra break line when read txt in C
  • recv() recieves incorrect data
  • How to make a string out of 3 user input strings in c
  • Can't find issue with "Too many arguments for format"
  • How to print \n on a string during a child process on a fork
  • redundant operations in disassembled c code
  • C output of hexadecimal division different from all other sources
  • C- getchar() re-read characters?
  • C : String comparing
  • Find the maximum sum of positive integers in an array in C language
  • Needs debugging symbols for __alignof__
  • Segmentation fault (core dumped) when using my own struct with pointers to array
  • Converting an uint8_t[] to an IP address string
  • Do the printf statements in this example invoke Undefined Behavior?
  • C Accept Client Socket "Invalid Argument" Error when adding Signal Thread
  • Another invert char array in C
  • A function, which contains a struct, that is declared in the following function?
  • Hashing int16_t to uint64_t
  • free() freezing in visual studio C
  • p is a pointer to a structure, what do all these code snippets do?
  • Android-NDK: Run time errors when using shmget, shmctl, shmdt in Android API 27
  • Is shifting the signed bit of a signed short undefined behaviour in C?
  • How to implement arithmetic right shift in C
  • What happen if passing the address of the stack variable to the pthread function?
  • How to add zeros after the significant digits after decimal in C?
  • C - strcspn() function skipping over the longer strings in my array
  • Do calls to shmget or shmat initialize data?
  • why `execv` can't use implicit convert from char** to char* const*?
  • Avoid pressing enter with getch() on linux ( GCC ) "No-echo"
  • A struct initialised in a function how can i use the values?
  • Facing problems with the atoi function if used repeatedly
  • Assigning a pointer to a larger array to a pointer to a smaller VLA
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org