logo
down
shadow

How to protect critical section in PHP?


How to protect critical section in PHP?

By : user25693
Date : November 21 2020, 07:01 PM
Does that help Use semaphores for that.
code :


Share : facebook icon twitter icon
How do I protect my Indy socket writes with a critical section?

How do I protect my Indy socket writes with a critical section?


By : user3878434
Date : March 29 2020, 07:55 AM
Hope this helps CS/mutex locks work just fine when used properly. Make sure that your threads are locking the same CS/mutex instance, not separate instances. Since you are sending the data from the server side, I would suggest using the OnConnect event to create a per-connection CS and store it in the TIdContext.Data property, and the OnDisconnect event to free it, eg:
code :
procedure TForm1.IdTCPServer1Connect(AContext: TIdContext);
begin
  AContext.Data := TCriticalSection.Create;
end;

procedure TForm1.IdTCPServer1Disconnect(AContext: TIdContext);
begin
  AContext.Data.Free;
  AContext.Data := nil;
end;
TCriticalSection(AContext.Data).Acquire;
try 
   AContext.Connection.Socket.Write(packet); 
finally 
  TCriticalSection(AContext.Data).Release;
end; 
type
  TMyContext = class(TIdServerContext)
  private
    CS: TCriticalSection;
  public
    constructor Create(AConnection: TIdTCPConnection; AYarn: TIdYarn; AList: TThreadList = nil); override;
    destructor Destroy; override;
    procedure SendPacket(const AData: TIdBytes); // or whatever parameters you need
  end;

constructor TMyContext.Create(AConnection: TIdTCPConnection; AYarn: TIdYarn; AList: TThreadList = nil);
begin
  inherited;
  CS := TCriticalSection.Create;
end;

destructor TMyContext.Destroy;
begin
  CS.Free;
  inherited;
end;

procedure TMyContext.SendPacket(const AData: TIdBytes);
begin
  CS.Acquire;
  try
    Connection.IOHandler.Write(AData);
  finally
    CS.Release;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  IdTCPServer1.ContextClass := TMyContext;
end;
TMyContext(AContext).SendPacket(packet); 
If the main thread holds a critical section, will a function attempting to acquire the critical section block if it's al

If the main thread holds a critical section, will a function attempting to acquire the critical section block if it's al


By : user44484
Date : March 29 2020, 07:55 AM
it helps some times Just use the debugger. Set a breakpoint in the startup code of your app, or in any message handler in your app, and check the current thread id in the debugger threads window. Then set a breakpoint in the function in question, and check the thread id when execution reaches that function. If they don't match, then the function is not being called on the same thread that your UI messages are running on.
While you're stopped in your function in the debugger, you can take a look at the call stack window to see the sequence of calls that led to your function call. This may provide some insight into how you got there, and how you got there on a different thread.
Spinlock not working to protect critical section on multi-core system

Spinlock not working to protect critical section on multi-core system


By : Scott
Date : March 29 2020, 07:55 AM
Any of those help Turns out the critical section was calling wait_for_completion_timeout. Even though the timeout was zero, it still slept and didn't wake up to release the spin lock if the interrupt occurred in the blocking section. Using try_wait_for_completion in this case resolved the issue.
I would have posted source but it spans many modules and has architecture abstractions for portability between operating systems. Would have been a mess.
How does a compiler knows not to move the code inside a critical section to the outside of the critical section?

How does a compiler knows not to move the code inside a critical section to the outside of the critical section?


By : Tom Byun
Date : March 29 2020, 07:55 AM
it fixes the issue Based on my understanding: ,
A compiler can do whatever re-ordering it wants to my code.
code :
f(); // Maybe affects memory somewhere.
g(); // Maybe affects memory somewhere.
void myfunc(X *ptr) {
    my_lock(ptr->mutex);   // Maybe affects memory somewhere.
    ptr->field++;          // Definitely affects memory at ptr->field.
    my_unlock(ptr->mutex); // Maybe affects memory somewhere.
}
int i = 5;
my_lock();   // Maybe affects memory somewhere.
i++;         // This is not "memory somewhere", this is my variable,
             // it's a register or on the stack and nobody else can
             // change it.
my_unlock(); // Maybe affects memory somewhere.
int i = 6;
my_lock();
my_unlock();
What happens if a thread is in the critical section or entering the critical section?

What happens if a thread is in the critical section or entering the critical section?


By : user2053194
Date : March 29 2020, 07:55 AM
Does that help For an example, let's assume that you have an array, and multiple threads that read and write to the array; and if different threads are reading and writing to the array at the same time they'd see inconsistent data and it'd cause problems. To prevent those problems you protect the array with some kind of lock - before doing anything with the array a thread acquires the array's lock, and when it's finished using the array the thread releases the array's lock.
For example:
code :
    acquire_array_lock();
    /** Critical section (code that does something with the array) **/
    release_array_lock();
acquire_lock(void) {
    while(myLock == 0) {
        // do nothing then retry
    }
    myLock = 1;
}

release_lock(void) {
    myLock = 0;
}
Related Posts Related Posts :
  • Which is the best php opensource e-commerce for handle 5 million prducts import daily?
  • $_SERVER['HTTP_X_REQUESTED_WITH'] seemingly not available on PHP 5.1.6
  • radio value lost by $_post array
  • Tool to add code highlighting and export to HTML
  • youtube video downloaded successfully on local, but on server(online) only 0 byte flv is downloading
  • Populating a Dropdown list in PHP dynamically
  • Why is turning magic_quotes_gpc on considered a bad practice?
  • Replace html text in javascript include
  • OOP Design - Where/When do you Validate properties?
  • RSA Encrypt in PHP to decrypt in .NET
  • Encrypting with Perl CBC and decrypting with PHP mcrypt
  • Code equivalence between Javascript and PHP
  • How to track DHL in our site from DHL site , by PHP
  • php resize image
  • How to use ^/$ if it's already used as a delimiter for regex in PHP?
  • Nobody owner (99 99) in FTP caused by php functions?
  • Php deployment to remote server using FTP from SVN after each commit
  • PHP: upload image max 150x150
  • How to implement Gmail OAuth API to send email (especially via SMTP)?
  • PHP comparion doesnt work..why?
  • Curl redirect,, not working?
  • SCORM lib for php
  • After form validation, how do you redirect to a new url w/ PHP
  • A solid nickname regexp
  • SVN checkout browser
  • Redirecting a large number of URLs with htaccess or php header
  • mail sent with php's mail() has blank From field
  • PHP intersection between array and object
  • Cannot get new product attribute in grid display
  • How do you make a count that counts the number of page-views? (PHP)
  • Reading data from a MySQL database into a HTML Table?
  • How to test PHP in MySQL Strict
  • Using a database class in my user class
  • C++ equivalent of PHP's pack()
  • splitting strings in php
  • Strange bug with PHP on Windows 7
  • PHP array question
  • How to get the age from a birthdate using PHP & MySQL?
  • Setting post tags in wordpress via XMLRPC API when submitting a post?
  • Generating random thumbnails with PHP+FFMPEG
  • what are the difference between for loop & for each loop in php
  • PHP FTP Upload thousands of files
  • How to get structed result using nested set in MySQL and PHP?
  • MYSQL Select and group by date
  • Detect base64 encoding in PHP?
  • Simple PHP GET question
  • What's the meaning of the PHP token name T_PAAMAYIM_NEKUDOTAYIM?
  • jquery GET and POST confusion
  • PNGs alpha transparancy in AS3 - Unknown file-type
  • how to use htaccess redirectmatch with query_string?
  • Creating a numerical order index on a MySQL table
  • mobile browsers' can't login to my site
  • Split PDF documents into separate pages using PHP (or possibly perl)
  • How to retrieve the value from Select html element using JS prototype in php?
  • How to execute Imagemagick in PHP
  • How do you make a function read form a txt file and store random lines in a variable?
  • Use fgetcsv for tab delimited file
  • PHP: What's the best way to check equality of $_SERVER['HTTP_REFERER']?
  • best way to escape and create a slug
  • Searching for a complex and well-designed PHP OOP application to learn from
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk