Click here to Skip to main content
15,913,709 members
Home / Discussions / C#
   

C#

 
QuestionSplit a number into multiple number in a specific way Pin
Mou_kol10-Aug-18 11:14
Mou_kol10-Aug-18 11:14 
AnswerRe: Number distribution in a specific way Pin
Mycroft Holmes10-Aug-18 13:34
professionalMycroft Holmes10-Aug-18 13:34 
AnswerRe: Number distribution in a specific way Pin
Luc Pattyn11-Aug-18 6:22
sitebuilderLuc Pattyn11-Aug-18 6:22 
QuestionGet file metada Pin
Member 1193763910-Aug-18 0:04
Member 1193763910-Aug-18 0:04 
AnswerRe: Get file metada Pin
Richard MacCutchan10-Aug-18 0:22
mveRichard MacCutchan10-Aug-18 0:22 
QuestionRDP connections in tabs Pin
Member 114416229-Aug-18 19:41
Member 114416229-Aug-18 19:41 
AnswerRe: RDP connections in tabs Pin
Mycroft Holmes9-Aug-18 20:20
professionalMycroft Holmes9-Aug-18 20:20 
GeneralRe: RDP connections in tabs Pin
Member 1144162212-Aug-18 23:54
Member 1144162212-Aug-18 23:54 
GeneralRe: RDP connections in tabs Pin
Pete O'Hanlon13-Aug-18 0:27
mvePete O'Hanlon13-Aug-18 0:27 
GeneralRe: RDP connections in tabs Pin
Member 1144162213-Aug-18 1:17
Member 1144162213-Aug-18 1:17 
GeneralRe: RDP connections in tabs Pin
Pete O'Hanlon13-Aug-18 1:23
mvePete O'Hanlon13-Aug-18 1:23 
GeneralRe: RDP connections in tabs Pin
Mycroft Holmes13-Aug-18 2:16
professionalMycroft Holmes13-Aug-18 2:16 
GeneralRe: RDP connections in tabs Pin
Member 1144162213-Aug-18 3:10
Member 1144162213-Aug-18 3:10 
GeneralRe: RDP connections in tabs Pin
Mycroft Holmes13-Aug-18 13:28
professionalMycroft Holmes13-Aug-18 13:28 
QuestionClasses In Threads - Pause One Until Another Finished Pin
Kevin Marois8-Aug-18 6:43
professionalKevin Marois8-Aug-18 6:43 
AnswerRe: Classes In Threads - Pause One Until Another Finished Pin
Richard Deeming8-Aug-18 8:05
mveRichard Deeming8-Aug-18 8:05 
AnswerRe: Classes In Threads - Pause One Until Another Finished Pin
OriginalGriff8-Aug-18 8:09
mveOriginalGriff8-Aug-18 8:09 
GeneralRe: Classes In Threads - Pause One Until Another Finished Pin
Kevin Marois8-Aug-18 8:22
professionalKevin Marois8-Aug-18 8:22 
GeneralRe: Classes In Threads - Pause One Until Another Finished Pin
OriginalGriff8-Aug-18 22:08
mveOriginalGriff8-Aug-18 22:08 
GeneralRe: Classes In Threads - Pause One Until Another Finished Pin
Richard Deeming9-Aug-18 0:48
mveRichard Deeming9-Aug-18 0:48 
Questionthreads Pin
Laurent mortroux6-Aug-18 22:44
Laurent mortroux6-Aug-18 22:44 
Hello

i créate 10 threads to do a task.
i create a Semapohre to limit access at compteur by 3 threads

how to lock data (compteur)

C#
namespace threads
{
    class Program
    {

        public static int compteur;
        public static object ob = new object();
        public static SemaphoreSlim sem = new SemaphoreSlim(3);

        public static void Thread_test() {
            sem.Wait();
            for (int i = 0; i < 1000000; i++)
            {
                compteur++;
            }
            sem.Release();
            System.Console.WriteLine(compteur.ToString());
        }

        static void Main(string[] args)
        {
            Thread[] th = new Thread[10];
            for (int i = 0; i < th.Length; i++)
            {
                th[i] = new Thread(Thread_test)
                {
                    Name = "thread_" + i
                };
                th[i].Start();
            }

            System.Console.ReadLine();
        }
    }
}


Result:

thread_2 = 1264634
thread_0 = 1440996
thread_1 = 1645012
thread_3 = 2344585
thread_5 = 2694858
thread_4 = 2809001
thread_6 = 3628891
thread_8 = 3651918
thread_7 = 3991965
thread_9 = 4527904


and i want this without lock function just with Semaphore:

thread_3 = 1893992
thread_1 = 2735536
thread_4 = 4274643
thread_2 = 4349873
thread_0 = 6248051
thread_5 = 7430503
thread_6 = 8107979
thread_7 = 8937401
thread_9 = 9815069
thread_8 = 10000000


Thank you so much
AnswerRe: threads Pin
OriginalGriff6-Aug-18 23:31
mveOriginalGriff6-Aug-18 23:31 
SuggestionRe: threads Pin
Richard Deeming7-Aug-18 2:29
mveRichard Deeming7-Aug-18 2:29 
AnswerRe: threads Pin
Richard Deeming7-Aug-18 2:28
mveRichard Deeming7-Aug-18 2:28 
GeneralRe: threads Pin
Laurent mortroux7-Aug-18 3:28
Laurent mortroux7-Aug-18 3:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.