Member Avatar

Hello, I wonder how we can use lock objects between 2 different applications. In the example below we can use lock objects to let only one thread to execute the code inside the lock object at a time. This works fine within one application. Now I wonder how we can use a lock object between 2 different applications. Is there some kind of Global system lock that can be used? I have put this code as illustration of what I try to find a solution for: object lockobject = new object(); //Application 1 void thread1() { Random rnd = new …

Member Avatar
0
28
Member Avatar

I have 2 processes (producer and consumer) sharing an int deque in shared memory, I have the producer process put 2 numbers in the deque and then it gets in a wait state losing its mutex lock. I then have the consumer process removing the numbers and printing them. It then does a notify on the condition which the producer is waiting on. The consumer then goes on its own wait on a second condition. After this case the producer does not wake up. I am using the same mutex between the processes. Please find all code below. Include file …

Member Avatar
0
22
Member Avatar

Hi, I have two threads A and B. Thread A prints only odd numbers. Thread B prints only even number. I want to synchronize it in such a way that on the screen I get continues numbers. How can I do that? I was thinking of using sockets. Thread A prints and sends a message to thread B. Thread B prints and then sends a message to thread A. Any thread before printing checks for the message from other thread and prints only if they have received the message. Is this the best design or any of you have a …

Member Avatar
0
30
Member Avatar

The assignment is to implement the Banker's Algorithm using pthreads and mutex locks. We implemented the Banker's Algorithm and it works fine. When we began implementing the pthreads and mutex locks we ran into problems. We know that each process is a thread, and that the mutex lock is acquired once it is inside of the Banker's Algorithm function and it is released once you leave the function. We are just confused about how the threads come into play.

Member Avatar
0
9
Member Avatar

Can I use semaphore or mutex on MikroC? If you know , please tell me the function name or exsample code.

Member Avatar
0
3
Member Avatar

Hi all! This is a simple piece of code that I wrote about a year ago, in response to a question on Daniweb. I decided to infuse it with some C++11 magic, which led to a pretty sweet code snippet, full of neat little techniques you might want to use, not to mention that this class can actually be pretty handy to have. There are a few similar implementations out-there, but they all use a different approach, my main inspiration is the scope-guard technique. This is an implementation of a simple wrapper class template that allows a variable to be …

Member Avatar
3
29
Member Avatar

Hi folks, I need a solution for following scenario. 1. Suppose two processes running simulteneously. 2. Process (P1) has already locked a mutex (M1). 3. Process (P2) is trying to lock same mutex (M1) but as it is already locked by (P1), P2 is waiting for unlocking (M1). 4. Now somehow process (P1) terminates or dies wihtout unlocking mutex (M1). 5. (P2) is still waiting for unlocking (M1). But as (P1) is dead now there is no other way to unlock the mutex. 6. In this situation, (P2) experiences a deadlock. How to resolve this kind of situation.

Member Avatar
0
4
Member Avatar

[CODE] sem_t w,r,s1,s2,s3; pthread_mutex_t m; int main(int argc, char* argv[]) { mutex and semaphores initialization ..... call threads.... wait until finish and cancel threads... clean threads and semaphores [I]exit;[/I] } void *reader(void *argv) { while(TRUE) { sem_wait(&r); sem_wait(&s1); pthread_mutex_lock(&m); ... read data..... pthread_mutex_unlock(&m); sem_post(&s1); sem_post(&w); } printf("Done read\n"); sem_post(&extra2); pthread_exit(NULL); } void *writer(void *argv) { while(TRUE) { sem_wait(&w); sem_wait(&s1); pthread_mutex_lock(&m); pthread_mutex_unlock(&m); sem_post(&s1); sem_post(&r); } printf("Done Write\n"); sem_post(&s3); pthread_exit(NULL); } [/CODE] I wrote a reader/writer issue and now it's writing the same as from the source ... but sometime it become unstable and making output wrong or stop working.. Can someone …

Member Avatar
0
3
Member Avatar
Reusable Code Snippet Run Only One Copy using Mutex

This snippet shows how to use Mutex to prevent multiple copies of your software from running. For a WinForms application, this code goes into the Program.cs file. The magic starts in line 14 where we attempt to create a Mutex. A Mutex is a form of a lock, but it has the ability to cross applications, not just locking within an application. We take advantage of this by creating one with a special name for our application (in this case "mydomain.com myprogramname"). You need to make sure this is unique to your software, so be as specific as you can. …

Member Avatar
5
4
Member Avatar

Hi there, I'm working on a project which incorporates the following: [LIST] [*]Reading data from XML [*]Writing data to XML [*]Interpreting XML and passing the information to various places [/LIST] These are on different threads, and I've used a Mutex to make sure they don't try opening the main XML file at the same time. My problem(s) are as follows: [LIST] [*]Despite calling the Dispose() method on exit, I still receive 'Error: Abandoned Mutex' on one of the Mutexes [*]The data doesn't seem to be being read properly. I doubt it even enters the loop, as I put a debugging …

Member Avatar
0
5
Member Avatar

Hi, I would like to make an threaded app which would use pthreads. I made a linked list and I put references to running threads here. I know that operations of adding a new thread reference to the list and removing a thread reference from the list after the thread finishes has to be mutexed. I just want to ask if I made the mutexes in right way. So only one thread can remove or add new item to the list. Lines 10-12 and 31-36. [CODE]/** * */ void *run_thread(void *soc) { t->number = (int) pthread_self(); printf("%d\n", t->number); /* unimportant …

Member Avatar
0
4
Member Avatar

I have a static function that write to a log file . In my project I am already using multithreading , hense I m getting exception that 'process can't access file because it is being used by another process ' I want to mutex that code so that if one thread is accessing that code then another will have to wait. Here is the code. [code] { private static Mutex mut = new Mutex(); mut.WaitOne(); StreamWriter sw = new StreamWriter(path + "\\" + logFileName, true); sw.WriteLine(stMessage); sw.Close(); mut.ReleaseMutex(); } [/code]

Member Avatar
0
6
Member Avatar

Basically I am trying to learn multithreading in C++. Platform: Windows XP. So far so good but I want to make sure I'm using mutex correctly. It compiles and runs as expected, but just wanted to make sure I use the CreateMutex() function in the correct place. Thanks! Here's my code (if it helps, I'm using Dev-C++): #include #include #include #include using namespace std; DWORD WINAPI myThreadFunc1(LPVOID param); DWORD WINAPI myThreadFunc2(LPVOID param); int readFile(); HANDLE hMutex; int main(int argc, char *argv[]) { hMutex = CreateMutex(NULL, false, "fileMutex"); HANDLE myThread1 = CreateThread(NULL, 0, myThreadFunc1, NULL, 0, NULL); HANDLE myThread2 = CreateThread(NULL, …

Member Avatar
0
2

The End.