dimanche 16 juillet 2017

Using same std::mutex & lock object everywhere for all instances of locking and unlocking in all parallel threads in C++

I've 2 threads which access a shared resource:

Shared resource is the below Doubly linked list:

std::list<RowColumns> ll;
struct RowColumns
{
   int x;
   float y;
   vector<vector<string> vec2D;
   .....//many other fields;
}

Thread1: Writes data to this shared resource.

Thread 2: Reads data from this shared resource and writes to a file.

Both threads run in parallel i.e. as and when data is written to shared resource by Thread1 , then thread2 should keep reading the data written and write it to a file, for every "X" time interval. So, the code of T1 & T2 has many statements where shared resource is accessed. So to avoid hijacking of the shared resource by 1 thread I just lock & unlock for every acess so that I can achieve concurrency and both threads get opportunity to run.

My query is can I use a same mutex variable everywhere for all instances of locking and unlocking. Example to make my question more clear:

In below example, as you can see, the same mutex object "singleMutex" is used & also the same "lock1" unique_lock() lock object is used in both T1 & T2 threads for all acceses of the different elements of the shared resource. Will Such usage of using the same mutex & the same lock across all lock & unlock instances work & really serve the mutex purpose ? As we know mutex variable & lock is not attached to any object or resource, i think it should work. BUt at the same time I was confuse dbecause, if we use same mutex everywhere then where do we need multiple mutextx ?

    std::mutex singleMutex;


Thread1():
{
RunStatement_WITH_NO_SHARED_RESOURCE ();
while(someCondition())
{
std::unique_lock<std::mutex> lock1(singleMutex);
RunStatement_ACCESSING_SHARED_RESOURCE();
lock1.unlock();
RunStatement_WITH_NO_SHARED_RESOURCE ();
RunStatement_WITH_NO_SHARED_RESOURCE ();
RunStatement_WITH_NO_SHARED_RESOURCE ();
lock1.lock();
RunStatement_ACCESSING_SHARED_RESOURCE();
lock1.unlock();
}
RunStatement_WITH_NO_SHARED_RESOURCE ();
RunStatement_WITH_NO_SHARED_RESOURCE ();
RunStatement_WITH_NO_SHARED_RESOURCE ();
std::unique_lock<std::mutex> lock1(singleMutex);
RunStatement_ACCESSING_SHARED_RESOURCE();
lock1.unlock();
RunStatement_WITH_NO_SHARED_RESOURCE ();
lock1.lock();
RunStatement_ACCESSING_SHARED_RESOURCE();
lock1.unlock();

RunStatement_WITH_NO_SHARED_RESOURCE ();
RunStatement_WITH_NO_SHARED_RESOURCE ();
RunStatement_WITH_NO_SHARED_RESOURCE ();
lock1.lock();
RunStatement_ACCESSING_SHARED_RESOURCE();
lock1.unlock();

Thread_2():

{
RunStatement_WITH_NO_SHARED_RESOURCE ();

std::unique_lock<std::mutex> lock1(singleMutex);
RunStatement_ACCESSING_SHARED_RESOURCE();
lock1.unlock();
RunStatement_WITH_NO_SHARED_RESOURCE ();
RunStatement_WITH_NO_SHARED_RESOURCE ();
RunStatement_WITH_NO_SHARED_RESOURCE ();
lock1.lock();
RunStatement_ACCESSING_SHARED_RESOURCE();
lock1.unlock();

RunStatement_WITH_NO_SHARED_RESOURCE ();
RunStatement_WITH_NO_SHARED_RESOURCE ();
RunStatement_WITH_NO_SHARED_RESOURCE ();

RunStatement_ACCESSING_SHARED_RESOURCE();
lock1.unlock();
RunStatement_WITH_NO_SHARED_RESOURCE ();
lock1.lock();
RunStatement_ACCESSING_SHARED_RESOURCE();
lock1.unlock();

RunStatement_WITH_NO_SHARED_RESOURCE ();
RunStatement_WITH_NO_SHARED_RESOURCE ();
RunStatement_WITH_NO_SHARED_RESOURCE ();
lock1.lock();
RunStatement_ACCESSING_SHARED_RESOURCE();
lock1.unlock();

Aucun commentaire:

Enregistrer un commentaire