LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-14-2011, 05:29 AM   #1
hjazz6
Member
 
Registered: Jan 2009
Posts: 36

Rep: Reputation: 0
Using lock in OpenMP and Pthreads


Hi,

I have a program that uses both Pthreads and OpenMP. Basically, 2 threads (Thread A and B) are created using Pthreads to do work, and in Thread A, OpenMP is used to parallelize a for loop.

If I have a global variable that is accessed by the OpenMP threads and also Thread B, can I use the lock in OpenMP to ensure I have no race conditions?

What I have in mind:

Code:
int count = 0;

pthread_create(&ThreadA, &attr, WorkA, NULL);
pthread_create(&ThreadB, &attr, WorkB, NULL);

void *WorkA (void *t)
{
   #pragma omp parallel for
   for (i = 0 ; i < N ; i++)
   {
      // Do some work
      #pragma omp critical
      {
         // Do some other work
         OMP_SET_LOCK(&lock);
         count++;
         OMP_UNSET_LOCK(&lock);
      }
   }
}

void *WorkB (void *t)
{
   if (count > 0)
   {
      OMP_SET_LOCK(&lock);
      count--;
      OMP_UNSET_LOCK(&lock);
      // Do some work
   }
}
Thank you.
 
Old 04-14-2011, 05:55 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by hjazz6 View Post
If I have a global variable that is accessed by the OpenMP threads and also Thread B, can I use the lock in OpenMP to ensure I have no race conditions?
Yes, so long as the `lock' variable you're using is the same one in both threads, that should work.
 
Old 04-14-2011, 06:52 PM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by hjazz6 View Post
If I have a global variable that is accessed by the OpenMP threads and also Thread B, can I use the lock in OpenMP to ensure I have no race conditions?
Yes, because omp_set_lock() and omp_unset_lock() are not OpenMP compiler directives, but actual locking functions.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] openMP for ubuntu mahmoodn Linux - Software 4 03-29-2011 04:06 AM
stunning OpenMP performance fatra2 Programming 6 05-10-2010 06:57 AM
Linking OpenMP Mash Programming 2 07-28-2007 02:44 PM
openmp in suse 10.2 ezhaar Programming 2 02-18-2007 12:37 PM
pthreads stack frame lock up detection flakjacket Linux - General 0 06-26-2004 11:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration