Synchronising threads

Ismael123

Member
Joined
May 13, 2016
Messages
14
Programming Experience
1-3
I have a thread which appends a 100 "_" into a file and another one a 100 "-" into the same file. Any idea how to synchronise them in order to get an output like _-_-_- etc.. I guess the solution will be similar in C#
C#:
Imports System.ThreadingImports System.IO


Module Module1
    Dim NesneMutex As New Mutex
    Dim fs As New FileStream("C:\\Data\\threadTesti.dat", FileMode.Append, FileAccess.Write)
    Sub alt()
        For i = 0 To 100
            fs.WriteByte(Asc("_"))
        Next
    End Sub


    Sub tire()
        For i = 0 To 100
            fs.WriteByte(Asc("-"))
        Next
    End Sub


    Sub Main()
        Dim t1 As New Thread(New ThreadStart(AddressOf alt))
        Dim t2 As New Thread(New ThreadStart(AddressOf tire))
        t1.Start()
        t2.Start()
        Console.ReadKey()
    End Sub
End Module
 
Is this something you really want to do or is it just a way to learn how to synchronise threads? If it's the latter then it seems like you're just asking us to do your work for you. What have you discovered in your research and what happened when you tried to implement it?
 
Im studying for a vb.net exams and 1 of the topics is threading. I tried solving many problems in vain and was hoping getting a hint or seeing the solution of this one will help me understand and solve other threading questions.And I really dont see how this reply of yours is suppose to be helpful
 
And I really dont see how this reply of yours is suppose to be helpful

Then you're not trying very hard. Would you like to just not get any replies or would you rather know why you're not getting any replies? If this was something you wanted to do in a real app then I would have advised against it because it's not something anything should do. If it's a contrived example for the sake of learning then that's different, but there is plenty of information out there on synchronising threads already so what you should be doing is researching for yourself first, using that information and then posting here if what you try doesn't work. Otherwise, you're just asking us to do your work for you. There's no rule against your doing that but many people don't like to be taken advantage of like that and expect an effort on your part first. You may encounter someone who is prepared to simply write your code for you but, if you don't, you wouldn't know why if no one told you. Maybe don't be so sensitive and accept that criticism is a normal thing. We all do things wrong so we should all expect to be criticised now and again.
 
Then you're not trying very hard. Would you like to just not get any replies or would you rather know why you're not getting any replies? If this was something you wanted to do in a real app then I would have advised against it because it's not something anything should do. If it's a contrived example for the sake of learning then that's different, but there is plenty of information out there on synchronising threads already so what you should be doing is researching for yourself first, using that information and then posting here if what you try doesn't work. Otherwise, you're just asking us to do your work for you. There's no rule against your doing that but many people don't like to be taken advantage of like that and expect an effort on your part first. You may encounter someone who is prepared to simply write your code for you but, if you don't, you wouldn't know why if no one told you. Maybe don't be so sensitive and accept that criticism is a normal thing. We all do things wrong so we should all expect to be criticised now and again.

Why do you assume I didnt do any research?Almost everything I found was related to blocking other threads while one is working. I struggled with this question and several others but couldnt solve it. Icame here for some help because Im trying to learn. Just giving a hint will not magically solve all my problems but it will at least help me understand thread synchronisation. And by the way the reason I responded to your criticism is because many programmers think beginners like me dont do any effort. Believe me I'd have never posted here if I found anything useful in my research.You could just have replied saying try using this , and I could have continued from there
Thanks for at least trying to understand me
 
Why do you assume I didnt do any research?
I don't. I can only go by what I see and I don't see any attempt to implement anything.
Almost everything I found was related to blocking other threads while one is working.
And that is exactly what you want to do here. One thread does its work and is then blocked while the other thread does its work and vice versa.

You've already got a Mutex in your code. Have you read the documentation for the Mutex class? It contains a code example. It shows how multiple threads can be forced to perform an operation one at a time. It doesn't take much of a leap to apply that to two threads performing an operation one at a time alternately.
 
I don't. I can only go by what I see and I don't see any attempt to implement anything.

And that is exactly what you want to do here. One thread does its work and is then blocked while the other thread does its work and vice versa.

You've already got a Mutex in your code. Have you read the documentation for the Mutex class? It contains a code example. It shows how multiple threads can be forced to perform an operation one at a time. It doesn't take much of a leap to apply that to two threads performing an operation one at a time alternately.



Ive tried using a mutex, a synclock, and a monitor before posting here but I was only able to pause the first thread and finish the second one. I guess it is because this multithreading still confuses me. And I deleted all of this before posting in order to have a cleaner code. Maybe that's why you think I'm just being lazy which in my opinion is not an assumption you should make before knowing the situation I was in before coming here.

And I also considered this post as part of a broader research. As I said before, my objective is not just getting a solution to this but instead using it as an example on thread synchronisation
 
Last edited:
The code in post 1 is VB.Net, but this is a C# forum. I recommend Visual Basic .NET Forums if you are working with VB.Net development.

Btw, my advice is to use two AutoResetEvent objects. Each thread wait its own AutoResetEvent, after write one thread signals the other and vice versa.
 
Back
Top Bottom